package com.buaa.pm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.pm.entity.ExtractResult;
import com.buaa.pm.entity.Paragraph;
import com.buaa.pm.mapper.ExtractResultMapper;
import com.buaa.pm.model.vo.ExtractRecordVO;
import com.buaa.pm.model.vo.ExtractResultVO;
import com.buaa.pm.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ExtractResultServiceImpl extends ServiceImpl<ExtractResultMapper, ExtractResult> implements ExtractResultService {

    @Autowired
    private ParagraphService paragraphService;

    @Autowired
    private ExtractRecordService extractRecordService;

    @Autowired
    private MarkedRecordService markedRecordService;

    @Override
    public List<ExtractResultVO> listExtractResultByTask(String taskId) {
        QueryWrapper<ExtractResult> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ExtractResult::getTaskId, taskId).eq(ExtractResult::getStatus, 1);
        List<ExtractResult> extractResultList = list(queryWrapper);
        List<String> paragraphIds = new ArrayList<>();
        List<ExtractResultVO> res = new ArrayList<>();
        for (ExtractResult extractResult : extractResultList) {
            ExtractResultVO extractResultVO = new ExtractResultVO();
            BeanUtils.copyProperties(extractResult, extractResultVO);
            extractResultVO.setPrompt(JSON.parseArray(extractResult.getPrompt()));
            paragraphIds.add(extractResult.getParagraphId());
            List<ExtractRecordVO> extractRecordVOList = extractRecordService.listExtractRecordByTask(taskId);
            List<ExtractRecordVO> filteredRecords = extractRecordVOList.stream()
                    .filter(record -> record.getParagraphId().equals(extractResult.getParagraphId()))
                    .collect(Collectors.toList());
            if(filteredRecords.size()>0){
                ExtractRecordVO extractRecordVO = filteredRecords.get(0);
                extractResultVO.setResults(extractRecordVO);
                res.add(extractResultVO);
            }
        }
        if (!paragraphIds.isEmpty()) {
            QueryWrapper<Paragraph> wrapper = new QueryWrapper<>();
            wrapper.lambda().in(Paragraph::getId, paragraphIds);
            List<Paragraph> paragraphList = paragraphService.list(wrapper);
            for (ExtractResultVO vo : res) {
                paragraphList.stream().filter(p -> p.getId().equals(vo.getParagraphId())).findAny().ifPresent(p -> {
                    vo.setParagraphContent(p.getContent());
                    vo.setPaperName(p.getPaperName());
                });
                paragraphList.stream()
                        .filter(p -> p.getId().equals(vo.getParagraphId()))
                        .findAny()
                        .ifPresent(p -> {
                            Optional<String> mofIdOptional = Optional.ofNullable(p.getMofId()); // 将mofId包装成Optional
                            mofIdOptional.ifPresent(mofId -> {
                                vo.setMarkedResults(markedRecordService.markedRecordByMofId(mofId));
                            });
                        });
            }
        }
        return res;
    }
}
